package com.th.supcom.pds.outdrugreturnapply.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRenderer;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.i18n.client.NumberFormat;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.widget.PhamBatchCombox;

/**
 * 退药信息面板
 * 
 * @author yzhang
 */
public class OutDrugReturnApplyDetailGridPanel extends ContentPanel
{
    private Grid <PdsSupplyDetailModel> m_grid;
    private ListStore <PdsSupplyDetailModel> m_store;
    private PhamBatchCombox m_combox;

    /**
     * 构造方法
     * 
     * @param drugReturnAndRefundFields
     * @param drugReturnButtons
     */
    public OutDrugReturnApplyDetailGridPanel ()
    {
        initUI ();
    }

    /**
     * 初始化界面
     */
    private void initUI ()
    {

        setHeaderVisible (false);
        setBorders (false);
        setLayout (new FitLayout ());
        m_combox = new PhamBatchCombox (60);
        m_combox.setEditable (false);
        m_store = new ListStore <PdsSupplyDetailModel> ();
        ColumnModel columnModels = createGridHeader ();
        columnModels.addAggregationRow (createAggregationRowConfig ());
        m_grid = new EditorGrid <PdsSupplyDetailModel> (m_store, columnModels);
        m_grid.getView ().setEmptyText ("暂时没有您需要的数据...");
        m_grid.setStripeRows (true);
        m_grid.setColumnLines (true);
        m_grid.getView ().setAutoFill (true);
        m_grid.setBorders (false);
        m_grid.setColumnReordering (true);
        add (m_grid);
        ToolBar toolBar = createColorShow ();
        ContentPanel bottomContainer = new ContentPanel ();
        bottomContainer.setHeaderVisible (false);
        bottomContainer.add (toolBar);
        setBottomComponent (bottomContainer);
    }

    /**
     * 颜色提示
     * 
     * @return
     */
    private ToolBar createColorShow ()
    {
        ToolBar toolBar;
        toolBar = new ToolBar ();
        String txt = "<span class='grayTendGrade'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可编辑</span>";
        Label lab = new Label (txt);
        lab.setStyleAttribute ("margin-left", "10px");
        toolBar.add (lab);
        return toolBar;
    }

    private ColumnModel createGridHeader ()
    {
        final List <ColumnConfig> columns = new ArrayList <ColumnConfig> ();
        RowNumberer r = new RowNumberer ();
        columns.add (r);

        ColumnConfig column = new ColumnConfig ();

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamName", "药品名称", HorizontalAlignment.LEFT, 120, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamSpec", "药品规格", HorizontalAlignment.LEFT, 60, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "unitQty", "包装数量", HorizontalAlignment.LEFT, 40, ClientUtils.renderForNumber ("unitQty"));
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "packageUnitName", "包装单位", HorizontalAlignment.LEFT, 40, null);
        columns.add (column);
        
        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "returnQty", "退药数量", HorizontalAlignment.RIGHT, 40, getwithdrawAmountRenderer ());
        column.setEditor (createCellEditor ());
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "packagePrice", "价格", HorizontalAlignment.LEFT, 40, ClientUtils.getRenderer ("packagePrice"));
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "costs", "金额", HorizontalAlignment.LEFT, 40, ClientUtils.getRenderer ("costs"));
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "usageCodName", "给药途径", HorizontalAlignment.LEFT, 40, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "performedByName", "发药药房", HorizontalAlignment.LEFT, 40, null);
        columns.add (column);

        return new ColumnModel (columns);
    }

    /**
     * 退药数量列渲染器
     * 
     * @return
     */

    private GridCellRenderer <PdsSupplyDetailModel> getwithdrawAmountRenderer ()
    {
        final GridCellRenderer <PdsSupplyDetailModel> change = new GridCellRenderer <PdsSupplyDetailModel> ()
        {

            @Override
            public Object render (final PdsSupplyDetailModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsSupplyDetailModel> store, final Grid <PdsSupplyDetailModel> grid)
            {
                config.cellAttr = "style='background-color:#E6E6F2; height:16px'";
                if (null == model.getReturnQty ())
                {
                    return null;
                }
                Double returnQty = model.getReturnQty ();
                return "<span style='float:right'>" +returnQty.intValue () + "</span>";
            }
        };
        return change;
    }

    /**
     * 编辑退药数量
     * 
     * @return
     */
    private CellEditor createCellEditor ()
    {

        final NumberField numberField = new NumberField ();
        numberField.setAllowNegative (false);
        numberField.setRegex ("^[0-9]*[0-9][0-9]*$");
        numberField.setPropertyEditorType (Integer.class);
        numberField.getPropertyEditor ().setFormat (NumberFormat.getFormat ("0"));
        return new CellEditor (numberField)
        {
            @Override
            public Object postProcessValue (Object value)
            {
                PdsSupplyDetailModel model = m_grid.getStore ().getAt (super.row);
                Double max = model.getUnitQty ();
                if (null == numberField.getValue ())
                {
                    return value;
                }
                if (numberField.getValue ().doubleValue () > max)
                {
                    model.setReturnQty (max);
                    Info.display ("提示", "输入数量不能大于原有数量！");
                    return String.valueOf (model.get ("returnQty"));
                }
                model.setReturnQty (numberField.getValue ().doubleValue ());
                return String.valueOf (model.get ("returnQty"));
            }

            @Override
            public Object preProcessValue (Object value)
            {
                if (null == value)
                {
                    return value;
                }
                else
                {
                    return Double.parseDouble ((String) value);
                }
            }

        };
    }

    /**
     * 设置 合计金额
     * 
     * @return
     */
    private AggregationRowConfig <PdsSupplyDetailModel> createAggregationRowConfig ()
    {
        final AggregationRowConfig <PdsSupplyDetailModel> config = new AggregationRowConfig <PdsSupplyDetailModel> ();
        config.setHtml ("tradeCosts", "退费金额:");
        // config.setSummaryType ("costs", SummaryType.SUM);
        config.setRenderer ("costs", new AggregationRenderer <PdsSupplyDetailModel> ()
        {
            @Override
            public Object render (final Number value, final int colIndex, final Grid <PdsSupplyDetailModel> grid, final ListStore <PdsSupplyDetailModel> store)
            {
                String displayValue = "0.00";
                List <PdsSupplyDetailModel> pdsSupplyDetailModels = store.getModels ();
                Double sum = 0.0;
                for (PdsSupplyDetailModel pdsSupplyDetailModel : pdsSupplyDetailModels)
                {
                    if (pdsSupplyDetailModel.getReturnQty () == null)
                    {
                        continue;
                    }
                    sum += pdsSupplyDetailModel.getPackagePrice () * pdsSupplyDetailModel.getReturnQty ();
                }
                displayValue = ClientUtils.formatNumTwo ((Double) sum);
                return "<span style='color:red;font-weight:bold;float:right;'>" + displayValue + "</span>";
            }
        });
        return config;
    }

    /**
     * 加载数据
     * 
     * @param models
     */
    public void refresh (List <PdsSupplyDetailModel> models)
    {
        m_store.removeAll ();
        m_store.add (models);
    }

    public Grid <PdsSupplyDetailModel> getGrid ()
    {
        return m_grid;
    }

    public ListStore <PdsSupplyDetailModel> getStore ()
    {
        return m_store;
    }

    /**
     * 获取表格中的models
     * 
     * @return
     */
    public List <PdsSupplyDetailModel> getModels ()
    {
        return m_store.getModels ();
    }

}
